package com.visualexpress.dao;

import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.Transaction;
import org.hibernate.criterion.Example;

import com.visualexpress.domain.Location;
import com.visualexpress.domain.LocationPairShipment;
import com.visualexpress.domain.RouteInfo;

/**
 * A data access object (DAO) providing persistence and search support for
 * LocationPairShipment entities. Transaction control of the save(), update()
 * and delete() operations can directly support Spring container-managed
 * transactions or they can be augmented to handle user-managed Spring
 * transactions. Each of these methods provides additional information for how
 * to configure it for the desired type of transaction control.
 * 
 * @see com.visualexpress.domain.LocationPairShipment
 * @author MyEclipse Persistence Tools
 */

public class LocationPairShipmentDAO extends BaseHibernateDAO {
	private static final Log log = LogFactory
			.getLog(LocationPairShipmentDAO.class);

	// property constants

	public void save(LocationPairShipment transientInstance) {
		log.debug("saving LocationPairShipment instance");
		try {
			Transaction t = this.getSession().beginTransaction();
			t.begin();
			getSession().save(transientInstance);
			t.commit();
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(LocationPairShipment persistentInstance) {
		log.debug("deleting LocationPairShipment instance");
		try {
			Transaction t = this.getSession().beginTransaction();
			t.begin();
			getSession().delete(persistentInstance);
			t.commit();
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

    public List<LocationPairShipment> findByLocPair(Location location1,Location location2)
    {
    	log.debug("query LocationPairShipment instance with location pair");
    	try{
    		String hql = "from LocationPairShipment as ri where ri.loc1.locationId=? and ri.loc2.locationId=?";
    		Query query = this.getSession().createQuery(hql);
    		query.setParameter(0, location1.getLocationId());
    		query.setParameter(1, location2.getLocationId());
    		List<LocationPairShipment> ris = query.list();
    		log.debug("query successful!");
    		return ris;
    	}catch (RuntimeException re) {
			log.error("query failed", re);
			throw re;
		}
    }
	
	public LocationPairShipment findById(java.lang.String id) {
		log.debug("getting LocationPairShipment instance with id: " + id);
		try {
			LocationPairShipment instance = (LocationPairShipment) getSession()
					.get("com.visualexpress.domain.LocationPairShipment", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(LocationPairShipment instance) {
		log.debug("finding LocationPairShipment instance by example");
		try {
			List results = getSession().createCriteria(
					"com.visualexpress.domain.LocationPairShipment").add(
					Example.create(instance)).list();
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding LocationPairShipment instance with property: "
				+ propertyName + ", value: " + value);
		try {
			String queryString = "from LocationPairShipment as model where model."
					+ propertyName + "= ?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findAll() {
		log.debug("finding all LocationPairShipment instances");
		try {
			String queryString = "from LocationPairShipment";
			Query queryObject = getSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public LocationPairShipment merge(LocationPairShipment detachedInstance) {
		log.debug("merging LocationPairShipment instance");
		try {
			LocationPairShipment result = (LocationPairShipment) getSession()
					.merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(LocationPairShipment instance) {
		log.debug("attaching dirty LocationPairShipment instance");
		try {
			Transaction t = this.getSession().beginTransaction();
			t.begin();
			getSession().saveOrUpdate(instance);
			t.commit();
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(LocationPairShipment instance) {
		log.debug("attaching clean LocationPairShipment instance");
		try {
			getSession().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}
}